-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Faster version of from_shape_fn and speed up ndarray-rand #728
Conversation
I idly wonder if we can just turn this around and implement internal iteration - |
One can also see that the Array::from_shape_fn(shape, |_| dist.sample(rng)) The whole thing is a wrapper for that + a method with a default for the |
I don't really like the name |
I think this is a good idea, and the implementation looks good. I agree, though, that |
0a62843
to
d8d76fa
Compare
Now it has the name |
This is like from_shape_fn, but avoids the overhead of iterating the indices for the elements - instead elements are just visited in memory order.
This simplifies random/random_using methods by skipping the index iteration in the constructor, which is unused anyway and only used for figuring out how many elments to make.
d8d76fa
to
235c83e
Compare
And let the closure take no argument. The rationale is that if a counter is needed, it can easily be added by the caller. It's a duplicate counter then, but with luck it could be conflated with the other by the compiler? Use it in Array::default, the other usages are not significant.
235c83e
to
c516803
Compare
Add new constructor from_shape_simple_fn
This is like from_shape_fn, but avoids the overhead of iterating the
indices for the elements - instead elements are just visited in memory
order.
Then, use from_shape_simple_fn in ndarray-rand, since
it doesn't care for indices, it generates items randomly.
Quick benchmarks (the existing ndarray-rand benches) show improvements, especially in the simpler uniform distribution: